package com.zhijian.medical.service.impl;

import cn.hutool.json.JSONUtil;
import com.zhijian.medical.dao.local.ArticleMapper;
import com.zhijian.medical.entity.po.Article;
import com.zhijian.medical.entity.po.ArticleHistory;
import com.zhijian.medical.entity.vo.request.ArticleSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.ArticleSearchVo;
import com.zhijian.medical.entity.vo.request.ArticleUpdateVo;
import com.zhijian.medical.entity.vo.response.ArticleVo;
import com.zhijian.medical.service.ArticleHistoryService;
import com.zhijian.medical.service.ArticleService;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.JsonUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 人员文章业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-03-19 14:59:34
 */
@Slf4j
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper mapper;

    @Autowired
    private ArticleHistoryService articleHistoryService;

    @Override
    public Article selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ArticleVo selectVoById(Long id) {
        ArticleVo articleVo = mapper.selectVoById(id);
        if (Objects.isNull(articleVo)) {
            return null;
        }
        String authorContent = articleVo.getAuthorContent();
        if (StringUtils.isNotBlank(authorContent)) {
            articleVo.setAuthorContentList(JsonUtil.toArray(authorContent, ArticleVo.AuthorContentVo.class));
        }
        return articleVo;
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(Article article) {
        Date now = new Date();
        article.setCreateTime(now);
        article.setUpdateTime(now);
        return mapper.insert(article) > 0 ? article.getId() : 0L;
    }

    @Override
    public Long insert(ArticleSaveOrUpdateVo articleSaveOrUpdateVo) {
        Article article = new Article();
        BeanUtils.copyProperties(articleSaveOrUpdateVo, article);
        article.setAuthorContent(JSONUtil.toJsonStr(articleSaveOrUpdateVo.getAuthorContentList()));
        return this.insert(article);
    }

    @Override
    public boolean update(Article article) {
        Date now = new Date();
        article.setUpdateTime(now);
        return mapper.updateByPrimaryKey(article) != 0;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public boolean update(ArticleUpdateVo articleUpdateVo) {
        Article oldArticle = this.selectById(articleUpdateVo.getId());
        AssertUtil.notNull(oldArticle, "此文章不存在或者已被删除");
        ArticleSaveOrUpdateVo article = new ArticleSaveOrUpdateVo();
        BeanUtils.copyProperties(articleUpdateVo, article);
        this.update(article);
        ArticleHistory articleHistory = new ArticleHistory();
        BeanUtils.copyProperties(articleUpdateVo, articleHistory);
        articleHistory.setArticleId(articleUpdateVo.getId());
        return articleHistoryService.insert(articleHistory) != 0;
    }

    @Override
    public boolean update(ArticleSaveOrUpdateVo articleSaveOrUpdateVo) {
        Article article = new Article();
        BeanUtils.copyProperties(articleSaveOrUpdateVo, article);
        article.setAuthorContent(JSONUtil.toJsonStr(articleSaveOrUpdateVo.getAuthorContentList()));
        return this.update(article);
    }

    @Override
    public Integer count(ArticleSearchVo articleSearchVo) {
        return mapper.count(articleSearchVo);
    }

    @Override
    public List<ArticleVo> selectList(ArticleSearchVo articleSearchVo) {
        return mapper.selectList(articleSearchVo);
    }

    @Override
    public Page<ArticleVo> selectPage(ArticleSearchVo articleSearchVo) {
        //先直接查总数
        int count = count(articleSearchVo);
        //分页
        Page<ArticleVo> page = new Page<>(true, count, articleSearchVo.getPageNum(), articleSearchVo.getPageSize());
        articleSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ArticleVo> responseVos = this.selectList(articleSearchVo);
        page.setData(responseVos);
        return page;
    }

}